<?php 
	require_once( '/nfs/bronfs/uwfs/dw00/d00/andreybr/wordpress/wp-load.php' );

	if ( is_user_logged_in() && checkRemoteFile($_POST["content"]))
	{
		$before = wp_count_posts();	// count of posts before post attempt
		//echo $before->publish;

		$current_user = wp_get_current_user();

		$post = array(
		'ID'             => '', //Are you updating an existing post?
		'menu_order'     => 0, //If new post is a page, it sets the order in which it should appear in the tabs.
		'comment_status' => 'open', // 'closed' means no comments.
		'ping_status'    => 'open', // 'closed' means pingbacks or trackbacks turned off
		'pinged'         => '', //?
		'post_author'    => $user_ID,
		//'post_category'  => [ array(<category id>, <...>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post's categories
		'post_content'   => '<img src=' . $_POST["content"] . '>', //The full text of the post.
		'post_excerpt'   => '', //For all your post excerpt needs.
		'post_name'      => 'the name',
		'post_parent'    => 0,
		'post_password'  => '', //password for post?
		'post_status'    => 'publish',
		'post_title'     => $_POST["title"],
		'post_type'      => 'post',
		'tags_input'     => '', //For tags.
		'to_ping'        => '', //?
		'tax_input'      => '', // support for custom taxonomies. 
		);

    	print_r ($post);
		wp_insert_post($post); 

		
		$args = array( 'numberposts' => '1' );
		$recent_post = wp_get_recent_posts( $args );
		//echo $recent_post[0]["ID"];

		$term = get_term_by('name', 'Image', 'category');
		//echo $term->term_id;
	
		wp_set_object_terms( $recent_post[0]["ID"], 10, 'category' );

		if($before->publish < wp_count_posts()->publish) // check if post count increased
		{
			//echo 'post succeded';
		}
		else
		{
			//echo 'post failed';
		}
	}
	else
	{
		echo 'not logged in or invalid image';
	}
	//wp_safe_redirect( 'http://students.washington.edu/andreybr/wordpress/?page_id=75' );

	function checkRemoteFile($url)
	{
   	 	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL,$url);
    	// don't download content
   		curl_setopt($ch, CURLOPT_NOBODY, 1);
    	curl_setopt($ch, CURLOPT_FAILONERROR, 1);
   		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   	 	if(curl_exec($ch)!==FALSE)
    	{
        	return true;
    	}
    	else
    	{
        	return false;
    	}
	}
?>

